package com.dream.testtubebaby.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.dream.testtubebaby.R;
import com.dream.testtubebaby.activity.DoctorDescActivity;
import com.dream.testtubebaby.adapter.HomeDoctorAdapter;
import com.dream.testtubebaby.data.doctor.Doctor;
import com.dream.testtubebaby.net.ServerStub;
import com.dream.testtubebaby.okhttp.OkHttpClientManager;
import com.dream.testtubebaby.utils.ToastUtil;
import com.squareup.okhttp.Request;

public class HomeDoctorFragment extends Fragment {
	public int newsType = 0;
	public GridView mGridView;
	private List<Doctor> mDatas = new ArrayList<Doctor>();
	public HomeDoctorAdapter mAdapter;

	public HomeDoctorFragment() {
	}

	public HomeDoctorFragment(int newsType) {
		this.newsType = newsType;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.home_doctor_item_fragment, null);
		mGridView = (GridView) view.findViewById(R.id.doctor_grid);
		initListView();
		// initProgressDialog();
		updateDoctorData();
		return view;
	}

	protected void initListView() {
		mAdapter = new HomeDoctorAdapter(getActivity(), mDatas,
				R.layout.horizontal_gridview_item);
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				long doctor_id = mDatas.get(position).getId();
				Intent intent = new Intent(getActivity(),
						DoctorDescActivity.class);
				intent.putExtra(DoctorDescActivity.DOCTOR_ID, doctor_id);
				startActivity(intent);
			}

		});
	}

	// private void initProgressDialog() {
	// mProgressDialog = new ProgressDialog(getActivity());
	// mProgressDialog.setMessage(getResources().getString(
	// R.string.xlistview_header_hint_loading));
	// }
	//
	// private void showProgressDialog() {
	// if (!mProgressDialog.isShowing()) {
	// mProgressDialog.show();
	// }
	// }
	//
	// private void dismissProgressDialog() {
	// if (mProgressDialog.isShowing()) {
	// mProgressDialog.dismiss();
	// }
	// }

	@Override
	public void onStart() {
		super.onStart();
	}

	private void updateDoctorData() {
		// showProgressDialog();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("type", String.valueOf(newsType));
		params.put("count", String.valueOf(HomeFragment.SEARCH_COUNT));
		OkHttpClientManager.postAsyn(ServerStub.BASEURL
				+ "m=Home&c=App&a=SearchDoctorTopN",
				new OkHttpClientManager.ResultCallback<String>() {

					@Override
					public void onError(Request request, Exception e) {
						ToastUtil.toast(getActivity(), R.string.server_error);
						// dismissProgressDialog();
					}

					@Override
					public void onResponse(String response) {
						mDatas.clear();
						try {
							JSONObject obj = new JSONObject(response);
							if (obj != null) {
								int result = obj.getInt("result");
								if (result == 0) {
									JSONArray myCases = new JSONArray(obj
											.getString("doctor"));
									for (int i = 0; i < myCases.length(); i++) {
										JSONObject jsonCase = (JSONObject) myCases
												.get(i);
										long id = jsonCase.getLong("id");
										String name = jsonCase
												.getString("name").trim();
										Doctor doctor = new Doctor();
										doctor.setId(id);
										doctor.setName(name);
										mDatas.add(doctor);
									}
								}
							}
						} catch (JSONException e) {
							ToastUtil.toast(getActivity(),
									R.string.server_error);
						}
						mAdapter.notifyDataSetChanged();
						// dismissProgressDialog();
					}
				}, params);
	}
}
